//
//  UIButton+Helper.m
//  LZLife
//
//  Created by 214 on 2021/3/16.
//

#import "UIButton+Helper.h"

@implementation UIButton (Helper)

- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space {
    /**
     *  知识点：titleEdgeInsets是title相对于其上下左右的inset，跟tableView的contentInset是类似的，
     *  如果只有title，那它上下左右都是相对于button的，image也是一样；
     *  如果同时有image和label，那这时候image的上左下是相对于button，右边是相对于label的；title的上右下是相对于button，左边是相对于image的。
     */
    
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith   = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;

    CGFloat labelWidth  = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
    // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth  = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth  = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值

    switch (style) {
        case ButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case ButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case ButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case ButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)changeBtnStringArray:(NSArray *)strArray
           colorArray:(NSArray *)colorArray
            fontArray:(NSArray *)fontArray{
    NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];
    NSString* _str = nil;
    UIFont*  _font = nil;
    NSRange _range = NSMakeRange(0, 0);
    for (int i=0; i<strArray.count; i++) {
        _str = strArray[i];
        NSUInteger location = [[mutAttStr string] rangeOfString:_str].location;
        NSUInteger length   = [[mutAttStr string] rangeOfString:_str].length;
        _range = NSMakeRange(location, length);
        // 改变颜色
        if (colorArray.count >= (i+1)) {
            [mutAttStr addAttribute:NSForegroundColorAttributeName value:colorArray[i] range:_range];
        }
        // 改变字体大小
        if (fontArray.count >= (i+1)) {
//            _font = [UIFont systemFontOfSize:[fontArray[i] floatValue]];
            _font = fontArray[i];
            [mutAttStr addAttribute:NSFontAttributeName value:_font range:_range];
        }
    }
    [self setAttributedTitle:mutAttStr forState:UIControlStateNormal];
}

-(void)timerWithParams:(NSDictionary *)param withUrl:(NSString *)url andBock:(void (^)(NSString *))resultBlock
{
    [CYPreviewTip showToast:@"验证码已发出"];
    [self startTime];
    if (resultBlock) {
        resultBlock(@"123456");
    }
}

#pragma  mark 开始计时
-(void)startTime
{
    __block int timeout=60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);     dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:@"重发验证码" forState:UIControlStateNormal];
                self.userInteractionEnabled = YES;
                [self setTitleColor:kColor999999 forState:UIControlStateNormal];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:[NSString stringWithFormat:@"重新获取(%ds)",timeout] forState:UIControlStateNormal];
                self.userInteractionEnabled = NO;
                [self setTitleColor:kColor999999 forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

/*UIButton setImage*/
+ (UIButton *)initWithButton:(CGRect)rect
                        text:(NSString *)title
                        font:(UIFont *)fontSize
                   textColor:(UIColor *)color
           selectedTextColor:(UIColor *)sColor
                   normalImg:(UIImage *)nImg
                     highImg:(UIImage *)hImg
                 selectedImg:(UIImage *)sImg{
    UIButton *setSubBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setSubBtn.frame = rect;
    setSubBtn.titleLabel.font = fontSize;
    [setSubBtn setTitle:title forState:UIControlStateNormal];
    [setSubBtn setTitleColor:color forState:UIControlStateNormal];
    [setSubBtn setTitleColor:sColor forState:UIControlStateSelected];
    if (nImg) {
        [setSubBtn setImage:nImg forState:UIControlStateNormal];
    }
    if (hImg) {
        [setSubBtn setImage:hImg forState:UIControlStateHighlighted];
    }
    if (sImg) {
        [setSubBtn setImage:sImg forState:UIControlStateSelected];
    }
    setSubBtn.backgroundColor = [UIColor clearColor];
    return setSubBtn;
}
@end
